<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: Customizing the CN Tracker</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div id="nav-path" class="navpath">
  <ul>
<li class="navelem"><a class="el" href="../../d3/d81/tutorial_contrib_root.html">Tutorials for contrib modules</a></li>  </ul>
</div>
</div><!-- top -->
<div class="header">
  <div class="headertitle">
<div class="title">Customizing the CN Tracker </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><h2>Goal </h2>
<p>In this tutorial you will learn how to</p>
<ul>
<li>Set custom parameters for CN tracker.</li>
<li>Use your own feature-extractor function for the CN tracker.</li>
</ul>
<p>This document contains tutorial for the <a class="el" href="../../d2/dff/classcv_1_1TrackerKCF.html">cv::TrackerKCF</a>.</p>
<h2>Source Code </h2>
<div class="fragment"><div class="line"><a name="l00001"></a><span class="lineno">    1</span>&#160;<span class="preprocessor">#include &lt;<a class="code" href="../../d1/d4e/utility_8hpp.html">opencv2/core/utility.hpp</a>&gt;</span></div><div class="line"><a name="l00002"></a><span class="lineno">    2</span>&#160;<span class="preprocessor">#include &lt;<a class="code" href="../../de/df6/contrib_2modules_2tracking_2include_2opencv2_2tracking_8hpp.html">opencv2/tracking.hpp</a>&gt;</span></div><div class="line"><a name="l00003"></a><span class="lineno">    3</span>&#160;<span class="preprocessor">#include &lt;<a class="code" href="../../dc/d3d/videoio_8hpp.html">opencv2/videoio.hpp</a>&gt;</span></div><div class="line"><a name="l00004"></a><span class="lineno">    4</span>&#160;<span class="preprocessor">#include &lt;<a class="code" href="../../d4/dd5/highgui_8hpp.html">opencv2/highgui.hpp</a>&gt;</span></div><div class="line"><a name="l00005"></a><span class="lineno">    5</span>&#160;<span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"><a name="l00006"></a><span class="lineno">    6</span>&#160;<span class="preprocessor">#include &lt;cstring&gt;</span></div><div class="line"><a name="l00007"></a><span class="lineno">    7</span>&#160;<span class="preprocessor">#include &quot;samples_utility.hpp&quot;</span></div><div class="line"><a name="l00008"></a><span class="lineno">    8</span>&#160;</div><div class="line"><a name="l00009"></a><span class="lineno">    9</span>&#160;<span class="keyword">using namespace </span>std;</div><div class="line"><a name="l00010"></a><span class="lineno">   10</span>&#160;<span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"><a name="l00011"></a><span class="lineno">   11</span>&#160;</div><div class="line"><a name="l00012"></a><span class="lineno">   12</span>&#160;<span class="comment">// prototype of the functino for feature extractor</span></div><div class="line"><a name="l00013"></a><span class="lineno">   13</span>&#160;<span class="keywordtype">void</span> sobelExtractor(<span class="keyword">const</span> <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> img, <span class="keyword">const</span> <a class="code" href="../../d2/d44/classcv_1_1Rect__.html">Rect</a> roi, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>&amp; feat);</div><div class="line"><a name="l00014"></a><span class="lineno">   14</span>&#160;</div><div class="line"><a name="l00015"></a><span class="lineno">   15</span>&#160;<span class="keywordtype">int</span> main( <span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>** argv ){</div><div class="line"><a name="l00016"></a><span class="lineno">   16</span>&#160;  <span class="comment">// show help</span></div><div class="line"><a name="l00017"></a><span class="lineno">   17</span>&#160;  <span class="keywordflow">if</span>(argc&lt;2){</div><div class="line"><a name="l00018"></a><span class="lineno">   18</span>&#160;    cout&lt;&lt;</div><div class="line"><a name="l00019"></a><span class="lineno">   19</span>&#160;      <span class="stringliteral">&quot; Usage: tracker &lt;video_name&gt;\n&quot;</span></div><div class="line"><a name="l00020"></a><span class="lineno">   20</span>&#160;      <span class="stringliteral">&quot; examples:\n&quot;</span></div><div class="line"><a name="l00021"></a><span class="lineno">   21</span>&#160;      <span class="stringliteral">&quot; example_tracking_kcf Bolt/img/%04d.jpg\n&quot;</span></div><div class="line"><a name="l00022"></a><span class="lineno">   22</span>&#160;      <span class="stringliteral">&quot; example_tracking_kcf faceocc2.webm\n&quot;</span></div><div class="line"><a name="l00023"></a><span class="lineno">   23</span>&#160;      &lt;&lt; endl;</div><div class="line"><a name="l00024"></a><span class="lineno">   24</span>&#160;    <span class="keywordflow">return</span> 0;</div><div class="line"><a name="l00025"></a><span class="lineno">   25</span>&#160;  }</div><div class="line"><a name="l00026"></a><span class="lineno">   26</span>&#160;</div><div class="line"><a name="l00027"></a><span class="lineno">   27</span>&#160;  <span class="comment">// declares all required variables</span></div><div class="line"><a name="l00028"></a><span class="lineno">   28</span>&#160;  <a class="code" href="../../d2/d44/classcv_1_1Rect__.html">Rect</a> roi;</div><div class="line"><a name="l00029"></a><span class="lineno">   29</span>&#160;  <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> frame;</div><div class="line"><a name="l00030"></a><span class="lineno">   30</span>&#160;</div><div class="line"><a name="l00032"></a><span class="lineno">   32</span>&#160;  <a class="code" href="../../db/dd1/structcv_1_1TrackerKCF_1_1Params.html">TrackerKCF::Params</a> param;</div><div class="line"><a name="l00033"></a><span class="lineno">   33</span>&#160;  param.<a class="code" href="../../db/dd1/structcv_1_1TrackerKCF_1_1Params.html#a478cf7e3a32518b4003f26624bd02680">desc_pca</a> = TrackerKCF::GRAY | TrackerKCF::CN;</div><div class="line"><a name="l00034"></a><span class="lineno">   34</span>&#160;  param.<a class="code" href="../../db/dd1/structcv_1_1TrackerKCF_1_1Params.html#a92346cbad37a3b82924f422d70261a65">desc_npca</a> = 0;</div><div class="line"><a name="l00035"></a><span class="lineno">   35</span>&#160;  param.<a class="code" href="../../db/dd1/structcv_1_1TrackerKCF_1_1Params.html#a1b5f5c8d18a9f66f4ad78573ac81d29e">compress_feature</a> = <span class="keyword">true</span>;</div><div class="line"><a name="l00036"></a><span class="lineno">   36</span>&#160;  param.<a class="code" href="../../db/dd1/structcv_1_1TrackerKCF_1_1Params.html#aff68ad20f63d588215fe57507b39394d">compressed_size</a> = 2;</div><div class="line"><a name="l00038"></a><span class="lineno">   38</span>&#160;</div><div class="line"><a name="l00039"></a><span class="lineno">   39</span>&#160;  <span class="comment">// create a tracker object</span></div><div class="line"><a name="l00041"></a><span class="lineno">   41</span>&#160;<span class="comment"></span>  <a class="code" href="../../dc/d84/group__core__basic.html#ga6395ca871a678020c4a31fadf7e8cc63">Ptr&lt;TrackerKCF&gt;</a> tracker = TrackerKCF::create(param);</div><div class="line"><a name="l00043"></a><span class="lineno">   43</span>&#160;</div><div class="line"><a name="l00045"></a><span class="lineno">   45</span>&#160;  tracker-&gt;setFeatureExtractor(sobelExtractor);</div><div class="line"><a name="l00047"></a><span class="lineno">   47</span>&#160;</div><div class="line"><a name="l00048"></a><span class="lineno">   48</span>&#160;  <span class="comment">// set input video</span></div><div class="line"><a name="l00049"></a><span class="lineno">   49</span>&#160;  std::string video = argv[1];</div><div class="line"><a name="l00050"></a><span class="lineno">   50</span>&#160;  <a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html">VideoCapture</a> cap(video);</div><div class="line"><a name="l00051"></a><span class="lineno">   51</span>&#160;</div><div class="line"><a name="l00052"></a><span class="lineno">   52</span>&#160;  <span class="comment">// get bounding box</span></div><div class="line"><a name="l00053"></a><span class="lineno">   53</span>&#160;  cap &gt;&gt; frame;</div><div class="line"><a name="l00054"></a><span class="lineno">   54</span>&#160;  roi=<a class="code" href="../../d7/dfc/group__highgui.html#ga8daf4730d3adf7035b6de9be4c469af5">selectROI</a>(<span class="stringliteral">&quot;tracker&quot;</span>,frame);</div><div class="line"><a name="l00055"></a><span class="lineno">   55</span>&#160;</div><div class="line"><a name="l00056"></a><span class="lineno">   56</span>&#160;  <span class="comment">//quit if ROI was not selected</span></div><div class="line"><a name="l00057"></a><span class="lineno">   57</span>&#160;  <span class="keywordflow">if</span>(roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6c16a3bce912faa4fe5be42d7f1b53fe">width</a>==0 || roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6fed06513cedd76652389e38c7b1222e">height</a>==0)</div><div class="line"><a name="l00058"></a><span class="lineno">   58</span>&#160;    <span class="keywordflow">return</span> 0;</div><div class="line"><a name="l00059"></a><span class="lineno">   59</span>&#160;</div><div class="line"><a name="l00060"></a><span class="lineno">   60</span>&#160;  <span class="comment">// initialize the tracker</span></div><div class="line"><a name="l00061"></a><span class="lineno">   61</span>&#160;  tracker-&gt;init(frame,roi);</div><div class="line"><a name="l00062"></a><span class="lineno">   62</span>&#160;</div><div class="line"><a name="l00063"></a><span class="lineno">   63</span>&#160;  <span class="comment">// perform the tracking process</span></div><div class="line"><a name="l00064"></a><span class="lineno">   64</span>&#160;  printf(<span class="stringliteral">&quot;Start the tracking process, press ESC to quit.\n&quot;</span>);</div><div class="line"><a name="l00065"></a><span class="lineno">   65</span>&#160;  <span class="keywordflow">for</span> ( ;; ){</div><div class="line"><a name="l00066"></a><span class="lineno">   66</span>&#160;    <span class="comment">// get frame from the video</span></div><div class="line"><a name="l00067"></a><span class="lineno">   67</span>&#160;    cap &gt;&gt; frame;</div><div class="line"><a name="l00068"></a><span class="lineno">   68</span>&#160;</div><div class="line"><a name="l00069"></a><span class="lineno">   69</span>&#160;    <span class="comment">// stop the program if no more images</span></div><div class="line"><a name="l00070"></a><span class="lineno">   70</span>&#160;    <span class="keywordflow">if</span>(frame.rows==0 || frame.cols==0)</div><div class="line"><a name="l00071"></a><span class="lineno">   71</span>&#160;      <span class="keywordflow">break</span>;</div><div class="line"><a name="l00072"></a><span class="lineno">   72</span>&#160;</div><div class="line"><a name="l00073"></a><span class="lineno">   73</span>&#160;    <span class="comment">// update the tracking result</span></div><div class="line"><a name="l00074"></a><span class="lineno">   74</span>&#160;    tracker-&gt;update(frame,roi);</div><div class="line"><a name="l00075"></a><span class="lineno">   75</span>&#160;</div><div class="line"><a name="l00076"></a><span class="lineno">   76</span>&#160;    <span class="comment">// draw the tracked object</span></div><div class="line"><a name="l00077"></a><span class="lineno">   77</span>&#160;    <a class="code" href="../../d6/d6e/group__imgproc__draw.html#ga07d2f74cadcf8e305e810ce8eed13bc9">rectangle</a>( frame, roi, <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>( 255, 0, 0 ), 2, 1 );</div><div class="line"><a name="l00078"></a><span class="lineno">   78</span>&#160;</div><div class="line"><a name="l00079"></a><span class="lineno">   79</span>&#160;    <span class="comment">// show image with the tracked object</span></div><div class="line"><a name="l00080"></a><span class="lineno">   80</span>&#160;    <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>(<span class="stringliteral">&quot;tracker&quot;</span>,frame);</div><div class="line"><a name="l00081"></a><span class="lineno">   81</span>&#160;</div><div class="line"><a name="l00082"></a><span class="lineno">   82</span>&#160;    <span class="comment">//quit on ESC button</span></div><div class="line"><a name="l00083"></a><span class="lineno">   83</span>&#160;    <span class="keywordflow">if</span>(<a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>(1)==27)<span class="keywordflow">break</span>;</div><div class="line"><a name="l00084"></a><span class="lineno">   84</span>&#160;  }</div><div class="line"><a name="l00085"></a><span class="lineno">   85</span>&#160;</div><div class="line"><a name="l00086"></a><span class="lineno">   86</span>&#160;  <span class="keywordflow">return</span> 0;</div><div class="line"><a name="l00087"></a><span class="lineno">   87</span>&#160;}</div><div class="line"><a name="l00088"></a><span class="lineno">   88</span>&#160;</div><div class="line"><a name="l00089"></a><span class="lineno">   89</span>&#160;<span class="keywordtype">void</span> sobelExtractor(<span class="keyword">const</span> <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> img, <span class="keyword">const</span> <a class="code" href="../../d2/d44/classcv_1_1Rect__.html">Rect</a> roi, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>&amp; feat){</div><div class="line"><a name="l00090"></a><span class="lineno">   90</span>&#160;    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> sobel[2];</div><div class="line"><a name="l00091"></a><span class="lineno">   91</span>&#160;    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> patch;</div><div class="line"><a name="l00092"></a><span class="lineno">   92</span>&#160;    <a class="code" href="../../d2/d44/classcv_1_1Rect__.html">Rect</a> region=roi;</div><div class="line"><a name="l00093"></a><span class="lineno">   93</span>&#160;</div><div class="line"><a name="l00095"></a><span class="lineno">   95</span>&#160;    <span class="comment">// extract patch inside the image</span></div><div class="line"><a name="l00096"></a><span class="lineno">   96</span>&#160;    <span class="keywordflow">if</span>(roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a2cadfdc3b4b7dbf8085622b27e044572">x</a>&lt;0){region.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a2cadfdc3b4b7dbf8085622b27e044572">x</a>=0;region.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6c16a3bce912faa4fe5be42d7f1b53fe">width</a>+=roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a2cadfdc3b4b7dbf8085622b27e044572">x</a>;}</div><div class="line"><a name="l00097"></a><span class="lineno">   97</span>&#160;    <span class="keywordflow">if</span>(roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6a4860e984df1752623b6ce2a8bde73a">y</a>&lt;0){region.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6a4860e984df1752623b6ce2a8bde73a">y</a>=0;region.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6fed06513cedd76652389e38c7b1222e">height</a>+=roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6a4860e984df1752623b6ce2a8bde73a">y</a>;}</div><div class="line"><a name="l00098"></a><span class="lineno">   98</span>&#160;    <span class="keywordflow">if</span>(roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a2cadfdc3b4b7dbf8085622b27e044572">x</a>+roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6c16a3bce912faa4fe5be42d7f1b53fe">width</a>&gt;img.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a>)region.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6c16a3bce912faa4fe5be42d7f1b53fe">width</a>=img.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a>-roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a2cadfdc3b4b7dbf8085622b27e044572">x</a>;</div><div class="line"><a name="l00099"></a><span class="lineno">   99</span>&#160;    <span class="keywordflow">if</span>(roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6a4860e984df1752623b6ce2a8bde73a">y</a>+roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6fed06513cedd76652389e38c7b1222e">height</a>&gt;img.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>)region.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6fed06513cedd76652389e38c7b1222e">height</a>=img.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>-roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6a4860e984df1752623b6ce2a8bde73a">y</a>;</div><div class="line"><a name="l00100"></a><span class="lineno">  100</span>&#160;    <span class="keywordflow">if</span>(region.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6c16a3bce912faa4fe5be42d7f1b53fe">width</a>&gt;img.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a>)region.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6c16a3bce912faa4fe5be42d7f1b53fe">width</a>=img.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a>;</div><div class="line"><a name="l00101"></a><span class="lineno">  101</span>&#160;    <span class="keywordflow">if</span>(region.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6fed06513cedd76652389e38c7b1222e">height</a>&gt;img.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>)region.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6fed06513cedd76652389e38c7b1222e">height</a>=img.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>;</div><div class="line"><a name="l00103"></a><span class="lineno">  103</span>&#160;</div><div class="line"><a name="l00104"></a><span class="lineno">  104</span>&#160;    patch=img(region).<a class="code" href="../../d3/d63/classcv_1_1Mat.html#adff2ea98da45eae0833e73582dd4a660">clone</a>();</div><div class="line"><a name="l00105"></a><span class="lineno">  105</span>&#160;    <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cvtColor</a>(patch,patch, <a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#gga4e0972be5de079fed4e3a10e24ef5ef0a353a4b8db9040165db4dacb5bcefb6ea">COLOR_BGR2GRAY</a>);</div><div class="line"><a name="l00106"></a><span class="lineno">  106</span>&#160;</div><div class="line"><a name="l00108"></a><span class="lineno">  108</span>&#160;    <span class="comment">// add some padding to compensate when the patch is outside image border</span></div><div class="line"><a name="l00109"></a><span class="lineno">  109</span>&#160;    <span class="keywordtype">int</span> addTop,addBottom, addLeft, addRight;</div><div class="line"><a name="l00110"></a><span class="lineno">  110</span>&#160;    addTop=region.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6a4860e984df1752623b6ce2a8bde73a">y</a>-roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6a4860e984df1752623b6ce2a8bde73a">y</a>;</div><div class="line"><a name="l00111"></a><span class="lineno">  111</span>&#160;    addBottom=(roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6fed06513cedd76652389e38c7b1222e">height</a>+roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6a4860e984df1752623b6ce2a8bde73a">y</a>&gt;img.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>?roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6fed06513cedd76652389e38c7b1222e">height</a>+roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6a4860e984df1752623b6ce2a8bde73a">y</a>-img.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>:0);</div><div class="line"><a name="l00112"></a><span class="lineno">  112</span>&#160;    addLeft=region.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a2cadfdc3b4b7dbf8085622b27e044572">x</a>-roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a2cadfdc3b4b7dbf8085622b27e044572">x</a>;</div><div class="line"><a name="l00113"></a><span class="lineno">  113</span>&#160;    addRight=(roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6c16a3bce912faa4fe5be42d7f1b53fe">width</a>+roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a2cadfdc3b4b7dbf8085622b27e044572">x</a>&gt;img.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a>?roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6c16a3bce912faa4fe5be42d7f1b53fe">width</a>+roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a2cadfdc3b4b7dbf8085622b27e044572">x</a>-img.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a>:0);</div><div class="line"><a name="l00114"></a><span class="lineno">  114</span>&#160;</div><div class="line"><a name="l00115"></a><span class="lineno">  115</span>&#160;    <a class="code" href="../../d2/de8/group__core__array.html#ga2ac1049c2c3dd25c2b41bffe17658a36">copyMakeBorder</a>(patch,patch,addTop,addBottom,addLeft,addRight,<a class="code" href="../../d2/de8/group__core__array.html#gga209f2f4869e304c82d07739337eae7c5aa1de4cff95e3377d6d0cbe7569bd4e9f">BORDER_REPLICATE</a>);</div><div class="line"><a name="l00117"></a><span class="lineno">  117</span>&#160;</div><div class="line"><a name="l00119"></a><span class="lineno">  119</span>&#160;    <a class="code" href="../../d4/d86/group__imgproc__filter.html#gacea54f142e81b6758cb6f375ce782c8d">Sobel</a>(patch, sobel[0], <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga4a3def5d72b74bed31f5f8ab7676099c">CV_32F</a>,1,0,1);</div><div class="line"><a name="l00120"></a><span class="lineno">  120</span>&#160;    <a class="code" href="../../d4/d86/group__imgproc__filter.html#gacea54f142e81b6758cb6f375ce782c8d">Sobel</a>(patch, sobel[1], <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga4a3def5d72b74bed31f5f8ab7676099c">CV_32F</a>,0,1,1);</div><div class="line"><a name="l00121"></a><span class="lineno">  121</span>&#160;</div><div class="line"><a name="l00122"></a><span class="lineno">  122</span>&#160;    <a class="code" href="../../d2/de8/group__core__array.html#ga7d7b4d6c6ee504b30a20b1680029c7b4">merge</a>(sobel,2,feat);</div><div class="line"><a name="l00124"></a><span class="lineno">  124</span>&#160;</div><div class="line"><a name="l00126"></a><span class="lineno">  126</span>&#160;    feat=feat/255.0-0.5; <span class="comment">// normalize to range -0.5 .. 0.5</span></div><div class="line"><a name="l00128"></a><span class="lineno">  128</span>&#160;<span class="comment"></span>}</div><div class="ttc" id="group__core__basic_html_ga599fe92e910c027be274233eccad7beb"><div class="ttname"><a href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">cv::Scalar</a></div><div class="ttdeci">Scalar_&lt; double &gt; Scalar</div><div class="ttdef"><b>Definition:</b> types.hpp:669</div></div>
<div class="ttc" id="videoio_8hpp_html"><div class="ttname"><a href="../../dc/d3d/videoio_8hpp.html">videoio.hpp</a></div></div>
<div class="ttc" id="group__imgproc__color__conversions_html_gga4e0972be5de079fed4e3a10e24ef5ef0a353a4b8db9040165db4dacb5bcefb6ea"><div class="ttname"><a href="../../d8/d01/group__imgproc__color__conversions.html#gga4e0972be5de079fed4e3a10e24ef5ef0a353a4b8db9040165db4dacb5bcefb6ea">cv::COLOR_BGR2GRAY</a></div><div class="ttdoc">convert between RGB/BGR and grayscale, color conversions </div><div class="ttdef"><b>Definition:</b> imgproc.hpp:551</div></div>
<div class="ttc" id="group__core__array_html_gga209f2f4869e304c82d07739337eae7c5aa1de4cff95e3377d6d0cbe7569bd4e9f"><div class="ttname"><a href="../../d2/de8/group__core__array.html#gga209f2f4869e304c82d07739337eae7c5aa1de4cff95e3377d6d0cbe7569bd4e9f">cv::BORDER_REPLICATE</a></div><div class="ttdoc">aaaaaa|abcdefgh|hhhhhhh </div><div class="ttdef"><b>Definition:</b> base.hpp:270</div></div>
<div class="ttc" id="classcv_1_1Rect___html_a2cadfdc3b4b7dbf8085622b27e044572"><div class="ttname"><a href="../../d2/d44/classcv_1_1Rect__.html#a2cadfdc3b4b7dbf8085622b27e044572">cv::Rect_::x</a></div><div class="ttdeci">_Tp x</div><div class="ttdoc">x coordinate of the top-left corner </div><div class="ttdef"><b>Definition:</b> types.hpp:453</div></div>
<div class="ttc" id="classcv_1_1Mat_html_abed816466c45234254d25bc59c31245e"><div class="ttname"><a href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">cv::Mat::rows</a></div><div class="ttdeci">int rows</div><div class="ttdoc">the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions ...</div><div class="ttdef"><b>Definition:</b> mat.hpp:2096</div></div>
<div class="ttc" id="structcv_1_1TrackerKCF_1_1Params_html_a478cf7e3a32518b4003f26624bd02680"><div class="ttname"><a href="../../db/dd1/structcv_1_1TrackerKCF_1_1Params.html#a478cf7e3a32518b4003f26624bd02680">cv::TrackerKCF::Params::desc_pca</a></div><div class="ttdeci">int desc_pca</div><div class="ttdoc">compressed descriptors of TrackerKCF::MODE </div><div class="ttdef"><b>Definition:</b> tracking.hpp:133</div></div>
<div class="ttc" id="group__core__basic_html_ga6395ca871a678020c4a31fadf7e8cc63"><div class="ttname"><a href="../../dc/d84/group__core__basic.html#ga6395ca871a678020c4a31fadf7e8cc63">cv::Ptr</a></div><div class="ttdeci">std::shared_ptr&lt; _Tp &gt; Ptr</div><div class="ttdef"><b>Definition:</b> cvstd_wrapper.hpp:23</div></div>
<div class="ttc" id="group__imgproc__draw_html_ga07d2f74cadcf8e305e810ce8eed13bc9"><div class="ttname"><a href="../../d6/d6e/group__imgproc__draw.html#ga07d2f74cadcf8e305e810ce8eed13bc9">cv::rectangle</a></div><div class="ttdeci">void rectangle(InputOutputArray img, Point pt1, Point pt2, const Scalar &amp;color, int thickness=1, int lineType=LINE_8, int shift=0)</div><div class="ttdoc">Draws a simple, thick, or filled up-right rectangle. </div></div>
<div class="ttc" id="highgui_8hpp_html"><div class="ttname"><a href="../../d4/dd5/highgui_8hpp.html">highgui.hpp</a></div></div>
<div class="ttc" id="group__highgui_html_ga453d42fe4cb60e5723281a89973ee563"><div class="ttname"><a href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">cv::imshow</a></div><div class="ttdeci">void imshow(const String &amp;winname, InputArray mat)</div><div class="ttdoc">Displays an image in the specified window. </div></div>
<div class="ttc" id="namespacecv_html"><div class="ttname"><a href="../../d2/d75/namespacecv.html">cv</a></div><div class="ttdoc">&quot;black box&quot; representation of the file storage associated with a file on disk. </div><div class="ttdef"><b>Definition:</b> affine.hpp:51</div></div>
<div class="ttc" id="group__highgui_html_ga8daf4730d3adf7035b6de9be4c469af5"><div class="ttname"><a href="../../d7/dfc/group__highgui.html#ga8daf4730d3adf7035b6de9be4c469af5">cv::selectROI</a></div><div class="ttdeci">Rect selectROI(const String &amp;windowName, InputArray img, bool showCrosshair=true, bool fromCenter=false)</div><div class="ttdoc">Allows users to select a ROI on the given image. </div></div>
<div class="ttc" id="classcv_1_1VideoCapture_html"><div class="ttname"><a href="../../d8/dfe/classcv_1_1VideoCapture.html">cv::VideoCapture</a></div><div class="ttdoc">Class for video capturing from video files, image sequences or cameras. </div><div class="ttdef"><b>Definition:</b> videoio.hpp:664</div></div>
<div class="ttc" id="group__imgproc__color__conversions_html_ga397ae87e1288a81d2363b61574eb8cab"><div class="ttname"><a href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cv::cvtColor</a></div><div class="ttdeci">void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)</div><div class="ttdoc">Converts an image from one color space to another. </div></div>
<div class="ttc" id="classcv_1_1Rect___html_a6a4860e984df1752623b6ce2a8bde73a"><div class="ttname"><a href="../../d2/d44/classcv_1_1Rect__.html#a6a4860e984df1752623b6ce2a8bde73a">cv::Rect_::y</a></div><div class="ttdeci">_Tp y</div><div class="ttdoc">y coordinate of the top-left corner </div><div class="ttdef"><b>Definition:</b> types.hpp:454</div></div>
<div class="ttc" id="structcv_1_1TrackerKCF_1_1Params_html_a92346cbad37a3b82924f422d70261a65"><div class="ttname"><a href="../../db/dd1/structcv_1_1TrackerKCF_1_1Params.html#a92346cbad37a3b82924f422d70261a65">cv::TrackerKCF::Params::desc_npca</a></div><div class="ttdeci">int desc_npca</div><div class="ttdoc">non-compressed descriptors of TrackerKCF::MODE </div><div class="ttdef"><b>Definition:</b> tracking.hpp:134</div></div>
<div class="ttc" id="group__imgproc__filter_html_gacea54f142e81b6758cb6f375ce782c8d"><div class="ttname"><a href="../../d4/d86/group__imgproc__filter.html#gacea54f142e81b6758cb6f375ce782c8d">cv::Sobel</a></div><div class="ttdeci">void Sobel(InputArray src, OutputArray dst, int ddepth, int dx, int dy, int ksize=3, double scale=1, double delta=0, int borderType=BORDER_DEFAULT)</div><div class="ttdoc">Calculates the first, second, third, or mixed image derivatives using an extended Sobel operator...</div></div>
<div class="ttc" id="group__core__array_html_ga2ac1049c2c3dd25c2b41bffe17658a36"><div class="ttname"><a href="../../d2/de8/group__core__array.html#ga2ac1049c2c3dd25c2b41bffe17658a36">cv::copyMakeBorder</a></div><div class="ttdeci">void copyMakeBorder(InputArray src, OutputArray dst, int top, int bottom, int left, int right, int borderType, const Scalar &amp;value=Scalar())</div><div class="ttdoc">Forms a border around an image. </div></div>
<div class="ttc" id="group__core__hal__interface_html_ga4a3def5d72b74bed31f5f8ab7676099c"><div class="ttname"><a href="../../d1/d1b/group__core__hal__interface.html#ga4a3def5d72b74bed31f5f8ab7676099c">CV_32F</a></div><div class="ttdeci">#define CV_32F</div><div class="ttdef"><b>Definition:</b> interface.h:78</div></div>
<div class="ttc" id="classcv_1_1Rect___html_a6fed06513cedd76652389e38c7b1222e"><div class="ttname"><a href="../../d2/d44/classcv_1_1Rect__.html#a6fed06513cedd76652389e38c7b1222e">cv::Rect_::height</a></div><div class="ttdeci">_Tp height</div><div class="ttdoc">height of the rectangle </div><div class="ttdef"><b>Definition:</b> types.hpp:456</div></div>
<div class="ttc" id="contrib_2modules_2tracking_2include_2opencv2_2tracking_8hpp_html"><div class="ttname"><a href="../../de/df6/contrib_2modules_2tracking_2include_2opencv2_2tracking_8hpp.html">tracking.hpp</a></div></div>
<div class="ttc" id="classcv_1_1Mat_html_aa3e5a47585c9ef6a0842556739155e3e"><div class="ttname"><a href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cv::Mat::cols</a></div><div class="ttdeci">int cols</div><div class="ttdef"><b>Definition:</b> mat.hpp:2096</div></div>
<div class="ttc" id="structcv_1_1TrackerKCF_1_1Params_html_a1b5f5c8d18a9f66f4ad78573ac81d29e"><div class="ttname"><a href="../../db/dd1/structcv_1_1TrackerKCF_1_1Params.html#a1b5f5c8d18a9f66f4ad78573ac81d29e">cv::TrackerKCF::Params::compress_feature</a></div><div class="ttdeci">bool compress_feature</div><div class="ttdoc">activate the pca method to compress the features </div><div class="ttdef"><b>Definition:</b> tracking.hpp:130</div></div>
<div class="ttc" id="classcv_1_1Rect___html"><div class="ttname"><a href="../../d2/d44/classcv_1_1Rect__.html">cv::Rect_</a></div><div class="ttdoc">Template class for 2D rectangles. </div><div class="ttdef"><b>Definition:</b> types.hpp:420</div></div>
<div class="ttc" id="utility_8hpp_html"><div class="ttname"><a href="../../d1/d4e/utility_8hpp.html">utility.hpp</a></div></div>
<div class="ttc" id="group__core__array_html_ga7d7b4d6c6ee504b30a20b1680029c7b4"><div class="ttname"><a href="../../d2/de8/group__core__array.html#ga7d7b4d6c6ee504b30a20b1680029c7b4">cv::merge</a></div><div class="ttdeci">void merge(const Mat *mv, size_t count, OutputArray dst)</div><div class="ttdoc">Creates one multi-channel array out of several single-channel ones. </div></div>
<div class="ttc" id="classcv_1_1Rect___html_a6c16a3bce912faa4fe5be42d7f1b53fe"><div class="ttname"><a href="../../d2/d44/classcv_1_1Rect__.html#a6c16a3bce912faa4fe5be42d7f1b53fe">cv::Rect_::width</a></div><div class="ttdeci">_Tp width</div><div class="ttdoc">width of the rectangle </div><div class="ttdef"><b>Definition:</b> types.hpp:455</div></div>
<div class="ttc" id="structcv_1_1TrackerKCF_1_1Params_html_aff68ad20f63d588215fe57507b39394d"><div class="ttname"><a href="../../db/dd1/structcv_1_1TrackerKCF_1_1Params.html#aff68ad20f63d588215fe57507b39394d">cv::TrackerKCF::Params::compressed_size</a></div><div class="ttdeci">int compressed_size</div><div class="ttdoc">feature size after compression </div><div class="ttdef"><b>Definition:</b> tracking.hpp:132</div></div>
<div class="ttc" id="classcv_1_1Mat_html_adff2ea98da45eae0833e73582dd4a660"><div class="ttname"><a href="../../d3/d63/classcv_1_1Mat.html#adff2ea98da45eae0833e73582dd4a660">cv::Mat::clone</a></div><div class="ttdeci">Mat clone() const CV_NODISCARD</div><div class="ttdoc">Creates a full copy of the array and the underlying data. </div></div>
<div class="ttc" id="classcv_1_1Mat_html"><div class="ttname"><a href="../../d3/d63/classcv_1_1Mat.html">cv::Mat</a></div><div class="ttdoc">n-dimensional dense array class </div><div class="ttdef"><b>Definition:</b> mat.hpp:801</div></div>
<div class="ttc" id="structcv_1_1TrackerKCF_1_1Params_html"><div class="ttname"><a href="../../db/dd1/structcv_1_1TrackerKCF_1_1Params.html">cv::TrackerKCF::Params</a></div><div class="ttdef"><b>Definition:</b> tracking.hpp:117</div></div>
<div class="ttc" id="group__highgui_html_ga5628525ad33f52eab17feebcfba38bd7"><div class="ttname"><a href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">cv::waitKey</a></div><div class="ttdeci">int waitKey(int delay=0)</div><div class="ttdoc">Waits for a pressed key. </div></div>
</div><!-- fragment --><h2>Explanation </h2>
<p>This part explains how to set custom parameters and use your own feature-extractor function for the CN tracker. If you need a more detailed information to use <a class="el" href="../../d0/d0a/classcv_1_1Tracker.html">cv::Tracker</a>, please refer to <a class="el" href="../../d2/d0a/tutorial_introduction_to_tracker.html">Introduction to OpenCV Tracker</a>.</p>
<ol type="1">
<li><p class="startli"><b>Set Custom Parameters</b></p>
<div class="fragment"><div class="line">  <a class="code" href="../../d4/d41/namespacecv_1_1dynafu.html#afe352b7045bf5f1496c2d8dd2a09a764">TrackerKCF::Params</a> param;</div><div class="line">  param.<a class="code" href="../../db/dd1/structcv_1_1TrackerKCF_1_1Params.html#a478cf7e3a32518b4003f26624bd02680">desc_pca</a> = TrackerKCF::GRAY | TrackerKCF::CN;</div><div class="line">  param.desc_npca = 0;</div><div class="line">  param.compress_feature = <span class="keyword">true</span>;</div><div class="line">  param.compressed_size = 2;</div></div><!-- fragment --><p> To set custom paramters, an object should be created. Each tracker algorithm has their own parameter format. So, in this case we should use parameter from <a class="el" href="../../d2/dff/classcv_1_1TrackerKCF.html">cv::TrackerKCF</a> since we are interested in modifying the parameter of this tracker algorithm.</p>
<p class="startli">There are several parameters that can be configured as explained in <a class="el" href="../../db/dd1/structcv_1_1TrackerKCF_1_1Params.html">cv::TrackerKCF::Params</a>. For this tutorial, we focussed on the feature extractor functions.</p>
<p class="startli">Several feature types can be used in <a class="el" href="../../d2/dff/classcv_1_1TrackerKCF.html">cv::TrackerKCF</a>. In this case, the grayscale value (1 dimension) and color-names features (10 dimension), will be merged as 11 dimension feature and then compressed into 2 dimension as specified in the code.</p>
<p class="startli">If you want to use another type of pre-defined feature-extractor function, you can check in <a class="el" href="../../d2/dff/classcv_1_1TrackerKCF.html#a2a97c5b832651524b8e782496b933d45">cv::TrackerKCF::MODE</a>. We will leave the non-compressed feature as 0 since we want to use a customized function.</p>
</li>
<li><p class="startli"><b>Using a custom function</b></p>
<p class="startli">You can define your own feature-extractor function for the CN tracker. However, you need to take care about several things:</p><ul>
<li>The extracted feature should have the same size as the size of the given bounding box (width and height). For the number of channels you can check the limitation in <a class="el" href="../../d3/d63/classcv_1_1Mat.html">cv::Mat</a>.</li>
<li>You can only use features that can be compared using Euclidean distance. Features like local binary pattern (LBP) may not be suitable since it should be compared using Hamming distance.</li>
</ul>
<p class="startli">Since the size of the extracted feature should be in the same size with the given bounding box, we need to take care whenever the given bounding box is partially out of range. In this case, we can copy part of image contained in the bounding box as shown in the snippet below.</p>
<div class="fragment"><div class="line">    <span class="comment">// extract patch inside the image</span></div><div class="line">    <span class="keywordflow">if</span>(roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a2cadfdc3b4b7dbf8085622b27e044572">x</a>&lt;0){region.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a2cadfdc3b4b7dbf8085622b27e044572">x</a>=0;region.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6c16a3bce912faa4fe5be42d7f1b53fe">width</a>+=roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a2cadfdc3b4b7dbf8085622b27e044572">x</a>;}</div><div class="line">    <span class="keywordflow">if</span>(roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6a4860e984df1752623b6ce2a8bde73a">y</a>&lt;0){region.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6a4860e984df1752623b6ce2a8bde73a">y</a>=0;region.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6fed06513cedd76652389e38c7b1222e">height</a>+=roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6a4860e984df1752623b6ce2a8bde73a">y</a>;}</div><div class="line">    <span class="keywordflow">if</span>(roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a2cadfdc3b4b7dbf8085622b27e044572">x</a>+roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6c16a3bce912faa4fe5be42d7f1b53fe">width</a>&gt;img.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a>)region.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6c16a3bce912faa4fe5be42d7f1b53fe">width</a>=img.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a>-roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a2cadfdc3b4b7dbf8085622b27e044572">x</a>;</div><div class="line">    <span class="keywordflow">if</span>(roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6a4860e984df1752623b6ce2a8bde73a">y</a>+roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6fed06513cedd76652389e38c7b1222e">height</a>&gt;img.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>)region.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6fed06513cedd76652389e38c7b1222e">height</a>=img.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>-roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6a4860e984df1752623b6ce2a8bde73a">y</a>;</div><div class="line">    <span class="keywordflow">if</span>(region.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6c16a3bce912faa4fe5be42d7f1b53fe">width</a>&gt;img.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a>)region.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6c16a3bce912faa4fe5be42d7f1b53fe">width</a>=img.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a>;</div><div class="line">    <span class="keywordflow">if</span>(region.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6fed06513cedd76652389e38c7b1222e">height</a>&gt;img.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>)region.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6fed06513cedd76652389e38c7b1222e">height</a>=img.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>;</div></div><!-- fragment --><p> Whenever the copied image is smaller than the given bounding box, padding should be given to the sides where the bounding box is partially out of frame.</p>
<div class="fragment"><div class="line">    <span class="comment">// add some padding to compensate when the patch is outside image border</span></div><div class="line">    <span class="keywordtype">int</span> addTop,addBottom, addLeft, addRight;</div><div class="line">    addTop=region.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6a4860e984df1752623b6ce2a8bde73a">y</a>-roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6a4860e984df1752623b6ce2a8bde73a">y</a>;</div><div class="line">    addBottom=(roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6fed06513cedd76652389e38c7b1222e">height</a>+roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6a4860e984df1752623b6ce2a8bde73a">y</a>&gt;img.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>?roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6fed06513cedd76652389e38c7b1222e">height</a>+roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6a4860e984df1752623b6ce2a8bde73a">y</a>-img.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>:0);</div><div class="line">    addLeft=region.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a2cadfdc3b4b7dbf8085622b27e044572">x</a>-roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a2cadfdc3b4b7dbf8085622b27e044572">x</a>;</div><div class="line">    addRight=(roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6c16a3bce912faa4fe5be42d7f1b53fe">width</a>+roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a2cadfdc3b4b7dbf8085622b27e044572">x</a>&gt;img.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a>?roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6c16a3bce912faa4fe5be42d7f1b53fe">width</a>+roi.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a2cadfdc3b4b7dbf8085622b27e044572">x</a>-img.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a>:0);</div><div class="line"></div><div class="line">    <a class="code" href="../../d2/de8/group__core__array.html#ga2ac1049c2c3dd25c2b41bffe17658a36">copyMakeBorder</a>(patch,patch,addTop,addBottom,addLeft,addRight,<a class="code" href="../../d2/de8/group__core__array.html#gga209f2f4869e304c82d07739337eae7c5aa1de4cff95e3377d6d0cbe7569bd4e9f">BORDER_REPLICATE</a>);</div></div><!-- fragment --></li>
<li><p class="startli"><b>Defining the feature</b></p>
<p class="startli">In this tutorial, the extracted feature is response of the Sobel filter in x and y direction. Those Sobel filter responses are concatenated, resulting a feature with 2 channels.</p>
<div class="fragment"><div class="line">    <a class="code" href="../../d4/d86/group__imgproc__filter.html#gacea54f142e81b6758cb6f375ce782c8d">Sobel</a>(patch, sobel[0], <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga4a3def5d72b74bed31f5f8ab7676099c">CV_32F</a>,1,0,1);</div><div class="line">    <a class="code" href="../../d4/d86/group__imgproc__filter.html#gacea54f142e81b6758cb6f375ce782c8d">Sobel</a>(patch, sobel[1], <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga4a3def5d72b74bed31f5f8ab7676099c">CV_32F</a>,0,1,1);</div><div class="line"></div><div class="line">    <a class="code" href="../../d2/de8/group__core__array.html#ga7d7b4d6c6ee504b30a20b1680029c7b4">merge</a>(sobel,2,feat);</div></div><!-- fragment --></li>
<li><p class="startli"><b>Post processing</b></p>
<p class="startli">Make sure to normalize the feature with range -0.5 to 0.5</p>
<div class="fragment"><div class="line">    feat=feat/255.0-0.5; <span class="comment">// normalize to range -0.5 .. 0.5</span></div></div><!-- fragment --></li>
</ol>
</div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:39 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
